package com.app.web.order.job;

import com.app.base.EnumType;
import com.app.utils.base.j2se.Common;
import com.app.utils.base.j2se.Convert;
import com.app.utils.base.j2se.DateUtil;
import com.app.web.order.OrderDao;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Wang on 2020/3/22.
 */
@Component
public class OrderStatus implements Job {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderAnalyService orderAnalyService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        success();
        over();
    }

    // 已发货15天的订单系统自动确认收货
    public void success(){
        Map<String,Object> param = new HashMap<>();
        param.put("status", EnumType.ORDER_STATUS.已发货.getStatus());
        String date = DateUtil.getBeforDate(15);
        param.put("date",date);
        List<Map<String,Object>> orders =  orderDao.getGoodsOrderList(param);
        if(Common.isNotEmpty(orders)){
            orderAnalyService.success(orders);
        }
    }

    // 确认收货10天的订单系统自动结算
    public void over(){
        Map<String,Object> param = new HashMap<>();
        param.put("status", EnumType.ORDER_STATUS.确认收货.getStatus());
        String date = DateUtil.getBeforDate(10);
        param.put("date",date);
        List<Map<String,Object>> orders =  orderDao.getGoodsOrderList(param);
        if(Common.isNotEmpty(orders)){
            orderAnalyService.over(orders);
            //TODO 所有完结订单给对应用户钱包添加佣金
        }
    }
}
